//
//  KsMain+CoreDataClass.swift
//  iKinds
//
//  Created by kinds on 2016/11/14.
//  Copyright © 2016年 Razeware LLC. All rights reserved.
//

import Foundation
import CoreData

@objc(KsMain)
public class KsMain: NSManagedObject {
	let errorDomain = "KsMainDomain"
	enum ErrorType:Int{
		case InvalidDate
		case NullString
	}
    
    func setType(_ type:type,enable:Bool){
        if enable{
            if let t = self.type{
                if !t.contains(type.rawValue.logo){
                    //确保关键类型logo放在第一个位置
                    if type == .critical{
                        self.type = type.rawValue.logo + t
                    }else{
                        self.type = t + type.rawValue.logo
                    }
                }
            }else{
                self.type = type.rawValue.logo
            }
        }else{
            if let t = self.type,t.contains(type.rawValue.logo){
                self.type = t.replacingOccurrences(of: type.rawValue.logo, with: "")
            }
        }
    }
    
    func makeTypeLongTerm(){
        if let start = dateBegin,let end = dateEnd{
            let r = Calendar.current.dateComponents([Calendar.Component.day], from: start, to: end)
            if let days = r.day, days >= 28{
                setType(.longTerm, enable: true)
            }else{
                setType(.longTerm, enable: false)
            }
        }
    }
    
    func calcScores(){
        level = 1
        self.difficulty = difficulty.easy.rawValue
        
        if hasType(.critical){
            level = 10
            self.difficulty = difficulty.veryHard.rawValue
        }else if hasType(.longTerm){
            self.difficulty = difficulty.hard.rawValue
        }else if hasType(.urgent){
            self.difficulty = difficulty.normal.rawValue
        }
    }
    
    func hasType(_ type:type)->Bool{
        if let t = self.type{
            return t.contains(type.rawValue.logo)
        }else{
            return false
        }
    }
	
	public override func validateForInsert() throws {
		try super.validateForInsert()
		
		if dateBegin == nil || dateEnd == nil{
			throw NSError(domain: errorDomain, code: ErrorType.InvalidDate.rawValue, userInfo: [NSLocalizedDescriptionKey:"dateBegin or dateEnd == nil"])
		}
		
		//if dateBegin! < Date(){
			//throw NSError(domain: errorDomain, code: ksMainErrorType.InvalidDate.rawValue, userInfo: [NSLocalizedDescriptionKey:"dateBegin < now"])
			//do nothing!!!
		if dateEnd!.timeIntervalSince(dateBegin!) <= 60{
			throw NSError(domain: errorDomain, code: ErrorType.InvalidDate.rawValue, userInfo: [NSLocalizedDescriptionKey:"dateEnd - dateBegin <= 60"])
		}else if title == nil || title!.characters.count == 0{
            throw NSError(domain: errorDomain, code: ErrorType.NullString.rawValue, userInfo: [NSLocalizedDescriptionKey:"title == nil"])
		}
        
        
	}
	
	enum difficulty:Int32 {
		case easy = 1
		case normal = 2
		case hard = 3
		case veryHard = 4
	}
	
	private enum TypeName:String{
		case longTerm = "LongTerm"
		case targeted = "Targeted"
		case allTheTime = "AllTheTime"
		case critical = "Critical"
		case urgent = "Urgent"
	}

	enum type:RawRepresentable{
		case longTerm,targeted,allTheTime,critical,urgent
		
		static let allTypes:[type] = [.longTerm,.targeted,.allTheTime,.critical,.urgent]
		
		typealias RawValue = (name:String,logo:String)
		var rawValue: RawValue{
			switch self{
			case .longTerm:
				return (TypeName.longTerm.rawValue,"📊")
			case .targeted:
				return (TypeName.targeted.rawValue,"🎯")
			case .allTheTime:
				return (TypeName.allTheTime.rawValue,"👁")
			case .critical:
				return (TypeName.critical.rawValue,"🚨")
			case .urgent:
				return (TypeName.urgent.rawValue,"⏰")
			}
		}
		
		init?(rawValue: type.RawValue) {
			switch rawValue.name{
			case TypeName.longTerm.rawValue:
				self = .longTerm
			case TypeName.targeted.rawValue:
				self = .targeted
			case TypeName.allTheTime.rawValue:
				self = .allTheTime
			case TypeName.critical.rawValue:
				self = .critical
			case TypeName.urgent.rawValue:
				self = .urgent
			default:
				return nil
			}
		}
	}
	
	enum type2:String {
		case longTerm = "longTerm_t"
		case targeted = "targeted_t"
		case allTheTime = "allTheTime_t"
		case critical = "critical_t"
	}
	
	enum timeUnit:String{
		case perMinute = "perMinute"
		case perHour = "perHour"
		case perDay = "perDay"
		case perMonth = "perMonth"
	}

}
